package com.dhh.sky.widget;

import android.content.Intent;

import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.dhh.sky.R;
import com.dhh.sky.activity.FilePickActivity;
import com.dhh.sky.service.UploadOperationService;
import com.dhh.sky.util.OperationManager;

public class FilePickActionMode implements ActionMode.Callback {
	private FilePickActivity context;
	private OperationManager om;
	
	public FilePickActionMode(FilePickActivity context){
		this.context = context;
		om = new OperationManager(context);
	}
	
	@Override
	public boolean onCreateActionMode(ActionMode mode, Menu menu) {
		MenuItem uploadItem = menu.add(context.getString(R.string.ab_upload));
		//uploadItem.setIcon(R.drawable.ic_upload);
		uploadItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				om.addUploadOperation(context.getItemNameSelected(), context.getTargetPath());				
				Intent intent = new Intent(context,UploadOperationService.class);
				context.startService(intent);
				context.finish();
				return true;
			}});
		uploadItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return true;
	}

	@Override
	public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
		return false;
	}

	@Override
	public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
		return false;
	}

	@Override
	public void onDestroyActionMode(ActionMode mode) {
		context.unSelectFile();
	}

}
